package com.twu.biblioteca.library;

import com.twu.biblioteca.IOStream.IO;
import com.twu.biblioteca.IOStream.Message;
import com.twu.biblioteca.book.Book;
import com.twu.biblioteca.book.BookStatus;
import com.twu.biblioteca.book.Books;
import com.twu.biblioteca.commands.AllCommands;
import com.twu.biblioteca.commands.ICommand;
import com.twu.biblioteca.customer.Customer;
import com.twu.biblioteca.customer.Customers;
import com.twu.biblioteca.movie.Movies;

import java.io.IOException;

public class Library {
    private static final String WELCOME = "Welcome!\n";
    private static final String SELECT_OPTION = "select one option:\n";
    private static final String INVALID_OPTION_MESSAGE = "Select a valid option!!\n";

    private static final String SUCCEED_TO_RESERVE_BOOK = "Thank You! Enjoy the book.\n";
    private static final String FAILED_TO_RESERVE_BOOK = "Sorry we don't have that book yet.\n";

    private static final String CHECK_MEMBERSHIP = "Please talk to a Librarian. Thank you.\n";

    private static final String WITHOUT_LOGIN_MESSAGE = "Please log in\n";
    private static final String SUCCEED_TO_LOGIN = "succeed to login.\n";
    private static final String FAILED_TO_LOGIN = "failed to login.\n";

    private static final String SUCCEED_TO_LOGOUT = "succeed to logout.\n";
    private static final String FAILED_TO_LOGOUT = "please login first.\n";

    private static final String BYE_BYE = "bye bye!\n";

    private Books books;
    private Movies movies;
    private Customers customers;
    private Customer currentCustomer;
    private AllCommands commands;
    private IO io;


    public Library(Books books, Movies movies, Customers customers, AllCommands commands, IO io) {
        this.books = books;
        this.movies = movies;
        this.customers = customers;
        this.io = io;
        this.commands = commands;
        this.currentCustomer = null;
    }

    public Customer getCurrentCustomer() {
        return this.currentCustomer;
    }

    public void setCurrentCustomer(Customer customer) {
        currentCustomer = customer;
    }

    public IO getIo() {
        return io;
    }

    public Message generateOutputMessage(String message) {
        return new Message(message);
    }

    public Message invalidOptionMessage() {
        return generateOutputMessage(INVALID_OPTION_MESSAGE);
    }

    public Message withoutLoginMessage() {
        return generateOutputMessage(WITHOUT_LOGIN_MESSAGE);
    }

    public Message ByeByeMessage() {
        return new Message(BYE_BYE);
    }

    public Message startMessage() throws IOException {
        String startMessage = WELCOME + SELECT_OPTION;
        for(int i = 1; i <= commands.getCommands().size(); i++) {
            startMessage = startMessage + i + ": " + commands.getCommandDescription(i - 1) + "\n";
        }
        return generateOutputMessage(startMessage);
    }

    public Message viewAllBooksMessage() {
        return books.viewAllBooksMessage();
    }

    public Message checkMembershipDetailsMessage() {
        if (currentCustomer == null)
            return generateOutputMessage(CHECK_MEMBERSHIP);
        else
            return generateOutputMessage(currentCustomer.toString());
    }

    public Message reserveBooksMessage(String bookId) {
        Book book = this.books.findBookById(Integer.parseInt(bookId));
        if (book != null && book.getStatus() == BookStatus.OnBookcase) {
            book.setStatus(BookStatus.Reserved);
            return generateOutputMessage(SUCCEED_TO_RESERVE_BOOK);
        } else
            return generateOutputMessage(FAILED_TO_RESERVE_BOOK);
    }

    public Message viewAllMoviesMessage() {
        return movies.viewAllMoviesMessage();
    }

    public Message customerLoginMessage(String libraryNumber, String password) {
        Customer customer = customers.checkCustomerAccount(libraryNumber, password);
        if (customer != null) {
            currentCustomer = customer;
            return generateOutputMessage(SUCCEED_TO_LOGIN);
        } else {
            currentCustomer = null;
            return generateOutputMessage(FAILED_TO_LOGIN);
        }
    }

    public Message customerLogoutMessage() {
        if (currentCustomer != null) {
            currentCustomer = null;
            return generateOutputMessage(SUCCEED_TO_LOGOUT);
        }
        else
            return generateOutputMessage(FAILED_TO_LOGOUT);
    }

    public boolean isValidCommand(ICommand command) {
        if(command != null && command.getCommandId() > 0 && command.getCommandId() <= commands.getCommands().size())
            return true;
        else
            return false;
    }

    public boolean isQuitCommand(ICommand command) {
        if (command.getCommandId() == commands.getCommands().size())
            return true;
        else
            return false;
    }
}
